/**
 * 根据传入时间格式，格式化传入时间
 * @param {String} time
 * @param {String} fmt
 * @returns {String}
 */
export const formatDate = function (time, fmt = 'yyyy-MM-dd HH:mm:ss') {
  if (!(time instanceof Date)) return time
  const week = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  const Q = ['一季度', '二季度', '三季度', '四季度']
  const o = {
    'M+': time.getMonth() + 1,
    'd+': time.getDate(),
    'H+': time.getHours(),
    'm+': time.getMinutes(),
    's+': time.getSeconds(),
    S: time.getMilliseconds(), // 毫秒
    q: Math.floor((time.getMonth() + 3) / 3), // 季度 (1)
    Q: Q[Math.floor((time.getMonth() + 3) / 3) - 1], // 季度 (一季度)
    w: time.getDay(), // 星期 (1)
    W: week[time.getDay()] // 星期 (星期天)
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (time.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
    }
  }
  return fmt
}

/**
 * 根据结束时间计算剩余时间，入参endTime为时间戳
 * @param {Number} endTime
 * @param {String} fmt
 * @returns {String}
 */
export const timeOut = function (endTime, fmt = 'mm:ss') {
  const nowTime = new Date().getTime()
  if (endTime < nowTime) return '00:00:00'
  const total = (endTime - nowTime) / 1000
  const o = {
    'd+': parseInt(total / 3600 / 24),
    'H+': parseInt((total / (60 * 60)) % 24),
    'm+': parseInt((total / 60) % 60),
    's+': parseInt(total % 60)
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
    }
  }
  return fmt
}

/**
 * 判断年份是否为润年
 * @param {Number} year
 * @returns {Boolean}
 */
export const isLeapYear = function (year) {
  return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)
}

/**
 * 获取某一年份的某一月份的天数
 * @param {Number} year
 * @param {Number} month
 * @returns {Number}
 */
export const getMonthDays = function (year, month) {
  return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (until.isLeapYear(year) ? 29 : 28)
}

/**
 * 获取某年的某天是第几周
 * @param {Number} y
 * @param {Number} m
 * @param {Number} d
 * @returns {Number}
 */
export const getWeekNumber = function (y, m, d) {
  const now = new Date(y, m - 1, d)
  const year = now.getFullYear()
  const month = now.getMonth()
  let days = now.getDate()
  for (let i = 0; i < month; i++) {
    days += getMonthDays(year, i)
  }
  const yearFirstDay = new Date(year, 0, 1).getDay() || 7
  let week = null
  if (yearFirstDay === 1) {
    week = Math.ceil(days / yearFirstDay)
  } else {
    days -= 7 - yearFirstDay + 1
    week = Math.ceil(days / 7) + 1
  }
  return week
}

//yyyy-MM-dd HH:mm:ss格式
export const timeFormatSeconds = function (time) {
  var d = time ? new Date(time) : new Date()
  var year = d.getFullYear()
  var month = d.getMonth() + 1
  var day = d.getDate()
  var hours = d.getHours()
  var min = d.getMinutes()
  var seconds = d.getSeconds()
  if (month < 10) month = '0' + month
  if (day < 10) day = '0' + day
  if (hours < 0) hours = '0' + hours
  if (min < 10) min = '0' + min
  if (seconds < 10) seconds = '0' + seconds
  return year + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + seconds
}
